<?php

class Jquery_Validator
{
	var $availableFunctions = array(
		'Annonceur::annonceurExistByName',
		'Annonceur_Agent::getAdvertiserCBForAuction',
        
		'Annonceur::getListAnnonceurByAgent',

		'Advertiser_Visibility::getCountryCBList',
		'Advertiser_Visibility::getEmplacementCBList',
		'Advertiser_Visibility::getKeywordList',

        'Emplacement::emplacementExistsByName',		
		'Emplacement::getEmplacementCBByInventory',
		'Emplacement::getEmplacementsInDemandByInventory',

        'Emplacement_Format::checkValid',
        
        'Emplacement_Visual::getVisual',
		'Emplacement_Visual::updateComent',
		'Emplacement_Visual::movePosition',

		'Campagne::existsName',
		'CPCMin::getCPCMin',
		'CPCMin_No_Keyword::getCPCMin',

		'Country::updateCountryStatus',
		'Country_Language::isExistLanguage',

		'Demand::isExistDemands',

		'Technical_Data_Content_Type::isExistName',
		'Technical_Data_Action_Type::isExistName',

		'Parameter::saveValue',

		'Format_Publicite::formatExistsByName',

		'Login::userExistByEmail',
		'Login::emailExistByLoginID',
		'Login::codeExistByLoginID',

		'Language::updateLanguageStatus',
		'Language::canDisable',

		'Inventory::isInventoryExistsByName',
		'Inventory::isInventoryExistsByCode',
		
		'Thematique::isExistsByName',

		'Currency::isExistsByName',

		'Keyword::validateInput',
		'Keyword::keywordExistsByName',
        'Keyword::searchList',
        'Keyword::getListLanguageNotAvailableByName', 
        'Keyword::isAcceptedKeywordName',
        'Keyword::isAcceptedKeywordSynonym',
        'Keyword::getLanguageCB',
        
		'Keyword_Synonym::isAvaliable',

		'Keyword_Emplacement::doesExist',
        'Keyword_Emplacement::listAssociatedKeyWordByEmplacement',

		'Keyword_Proposal_History::sendMessage'
	);

	var $classInvalid = false;
	var $objIncorrect = false;
	var $classDoesNotExist = false;
	var $methodDoseNotExist = false;

	function checkValidate( $objectfunction )
	{
		//check if the function allowed calling
		if( !in_array($objectfunction, $this->availableFunctions) )
		{
			$this->classInvalid = true;
			return false;
		}

		//check does the object and function posted by client is in correct form
		if( !preg_match('/^[0-9A-Za-z_]+::[0-9A-Za-z_]+$/', $objectfunction))
		{
				$this->objIncorrect = true;
			return false;
		}

		list($classname, $functionname) = explode('::', $objectfunction);

		if( !class_exists($classname) )
		{
				$this->classDoesNotExist = true;
				return false;
		}

		$object = new $classname();

		if( !method_exists($object, $functionname))
		{
				$this->methodDoseNotExist = true;
				return false;
		}

		return array("object"  => $object, "functionname" => $functionname);
	}

	function callFunction($objectfunction, $agrs)
	{
		$result = $this->checkValidate($objectfunction);
		if( !$result )
			return false;

		list($classname, $funcName) = explode('::', $objectfunction);
		return call_user_func_array( array( $result['object'], $result['functionname']), $agrs);
	}
}
